/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/* 
 * Copyright (c) 2009 City University of Hong Kong
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License m_version 2 as
 * published by the Free Software Foundation;
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 * 
 * Author: Wilson Thong (wilsonwk@ee.cityu.edu.hk) 
 */
#ifndef UNICAST_SESSION_MONITOR_HELPER
#define UNICAST_SESSION_MONITOR_HELPER

#include "jitter-sim-include.h"
#include "unicast-session-monitor.h"
#include "container-helper.h"
#include "ns3/callback.h"
#include <list>

namespace ns3 {
namespace eecityu {

using namespace std;

/**
 * \brief Help doing calculation on a list of unicast session monitor 
 * objects or a unicast session monitor itself
 */
class UnicastSessionMonitorHelper : public ContainerHelper
{  
public:
  typedef list < Ptr<UnicastSessionMonitor> > UnicastSessionMonitorList_t;
  /**
   * \brief Get the sum of all values returned by a specific method of 
   * each unicast session monitor in a list
   *
   * \param start_it The starting iterator of the list of unicast session monitor objects
   * \param end_it The ending iterator of the list of unicast session monitor objects
   * \param mem_ptr A pointer pointing to the specific method used to 
   * calculate the sum
   * \return The sum of all values returned by the method of each unicast
   * session monitors in the list
   */
  template <typename R> static R 
    GetSumAll (UnicastSessionMonitorList_t::const_iterator start_it,
               UnicastSessionMonitorList_t::const_iterator end_it, 
               R (UnicastSessionMonitor::*mem_ptr) () const);
  /**
   * \brief Call the method of all the unicast session monitor objects
   * 
   * \param start_it The starting iterator of the collection of the
   * unicast session monitors
   * \param end_it The ending iterator of the collection of the
   * unicast session monitors
   */
  template <typename R, typename T> static void
    RunAll (UnicastSessionMonitorList_t::const_iterator start_it,
            UnicastSessionMonitorList_t::const_iterator end_it, 
            R (UnicastSessionMonitor::*mem_ptr) ());
};

template <typename R> R 
UnicastSessionMonitorHelper::GetSumAll 
  (UnicastSessionMonitorList_t::const_iterator start_it,
   UnicastSessionMonitorList_t::const_iterator end_it, 
   R (UnicastSessionMonitor::*mem_ptr) () const)
{
  //NS_LOG_FUNCTION (monList << mem_ptr);

  // The value to be summed
  R sumValue = 0;  
  // walk through all unicast session monitor objects in the 'monList' and run their 'mem_ptr'
  // method to get the to-be-sum value
  for (UnicastSessionMonitorList_t::const_iterator it = start_it; it != end_it; it++)
  {
    Callback<R> cb;
    cb.Assign (MakeCallback (mem_ptr, PeekPointer (*it)));
    sumValue += cb ();
  }

  return sumValue;
}

template <typename R, typename T> void
UnicastSessionMonitorHelper::RunAll (UnicastSessionMonitorList_t::const_iterator start_it,
          UnicastSessionMonitorList_t::const_iterator end_it, 
          R (UnicastSessionMonitor::*mem_ptr) ())
{
  // walk through all unicast session monitor objects and run their 'mem_ptr' method 
  for (UnicastSessionMonitorList_t::const_iterator it = start_it; it != end_it; it++)
  {
    Callback<R> cb;
    cb.Assign (MakeCallback (mem_ptr, PeekPointer (*it)));
    cb ();
  }
}

} // namespace eecityu
} // namespace ns3

#endif // #ifndef UNICAST_SESSION_MONITOR_HELPER
